#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: shijianbo
@Email:
@Time: 2022/8/19 17:02
@File: basic.py
"""
from copy import deepcopy
from requests import session
from Common.otherUtils import merge
from BladeFerm.utils.OperationExcel import OperationExcel, ExcelVarles

'''
离职交接:
用例上传blade的接口代码
TEMPLATEDATA为报文模版
USER为各个组员的blade授权码
ENV为系统的一些配置
主要对外开放的是两个成员方法:
1.run_single,单个导入,入参可以看最下面示例的data
2.run_excel,以excel运行,入参一个excel的路径,excel模版就在同目录下
**日志的部分都先注释了,需要可以自己打开
'''

class BladeUpload(object):
    '''
    blade导入案例
    '''
    TEMPLATEDATA = {
        "api_token": "",# blade授权码
        "account": "",  # 授权码用户
        "vid": "",
        "vName": "",
        "nodePath": "",  # 路径
        "data": [{
            "caseName": "testUnit",  # 案例名
            "caseType": "1",  # 案例类型 1-手工,2-自动化
            "casePrior": "P1",  # 优先级P0-3
            "caseSideType": "0",  # 正例 or 反例
            "caseDetailType": "0",  # 用例类型 0-功能,1-性能,2-文档,3-安全,4-兼容性,5-可靠性,6-用户体验,7-安装部署
            "caseLayer": "1",  # 用例分层 0-系统级,1-模块级
            "caseHeader": "",  # 用例责任人
            "caseReviewStatus": "0",
            "caseReviewBy": "",  # 用例审核人
            "caseKeyword": "",
            "accurateWord": "",
            "caseDesc": "",  # 用例描述
            "testPointName": "",  # 测分
            "reqNum": "",  # 需求
            "taskNum": "",  # 任务
            "modifyNum": "",  # 修改单
            "step": [{
                "stepName": "",  # 步骤名称
                "stepJson": {
                    "stepDes": "",  # 步骤描述
                    "inputData": "",  # 输入数据
                    "Expect": "",  # 预期结果
                    "precisionTest": "0",
                    # "user_key_f6342dfec9cc4d70b53f876": "",  # 前置条件*
                    "user_key_operator": "",
                    "preSqlContent": [{
                        "id": "",
                        "type": "",
                        "content": "",
                        "connection": "",
                    }],
                    "scriptContent": "c647c4b6c79040a8bce4c723439ebcc4",  # 脚本ID
                    "scriptPath": "",  # 脚本路径
                    "dataContent": {  # 数据参数
                        "id": "",
                        "dataChoseRow": "",
                        "content": "",
                        "dataArrContent": [{
                            "Sheet1": [
                                ["1", "序号", "期望",'excelName','sheetName','caseName'],
                                ["2", "1", "","","",""],
                                ["3", "2", "",'111','222','333']
                            ],
                        }],
                    },
                    "checkContent": [{
                        "checkConnection": "",
                        "DBExpect": "",
                        "DBCheckSQL": "",
                        "DBSqlId": "",
                        "DBSqlName": "",
                    }],
                    "stepExcute": "是",
                },
            }],
            "caseState": "1",  # 0-待设计,1-审核通过,2-审核不通过,3-免审核,4-未提交待审核
        }],
    }
    USER={
        'nk4.0':{
            'sunjj25767':'JA8vlvTM4rOrndAWYYp8RytRdr8HA3_dcw_u7iG7g-q5DG7y6e78mx2AvYtnMp9NbXCB_Dg5_YB6tC28vdzN7Q',
            'chenwq28514':'JA8vlvTM4rOrndAWYYp8RytRdr8HA3_dcw_u7iG7g-pvE5NgdrxMzgqHQMjCQcvPSQI-VUpH7kg4xH78illmlg',
            'dujq47197':'JA8vlvTM4rOrndAWYYp8RytRdr8HA3_dcw_u7iG7g-ouJfeoqGZjYXANG1nzaKGs2ZrB49EYAzlE2GtIMwcw6g',
            'gansc21420':'JA8vlvTM4rOrndAWYYp8RytRdr8HA3_dcw_u7iG7g-oqtOnRqjLmLmq34Alf-q-tlUeTGh3MU45UnumZ8MeNww',
            'huzh32405':'JA8vlvTM4rOrndAWYYp8RytRdr8HA3_dcw_u7iG7g-rZMBh5H3bGI5Iyg2fG2dgJZOUAbR0Is5OVwIi_mxkwCQ',
            'lijl31833':'JA8vlvTM4rOrndAWYYp8RytRdr8HA3_dcw_u7iG7g-oZ4_g-tZAv_cEVcWmr_wlOMAF8yt86uLQy4iEWymRLoQ',
            'zhangxl29470':'JA8vlvTM4rOrndAWYYp8RytRdr8HA3_dcw_u7iG7g-oZqLnDUK52pa8hGpiycGLK_lDc3iw0xAJbPWXOQa5YFg',
            'zhangyuan20788':'JA8vlvTM4rOrndAWYYp8RytRdr8HA3_dcw_u7iG7g-o6z7DBLKmMnwHd1-qYOkhOjiceftnlytNIsCmCZvhW2g',
        },
        'nk5.0':{
            'sunjj25767': 'wDsTRTkM9GUKqHI9HXx7W6kbeWSa3Iqidu_eI6banLS5DG7y6e78mx2AvYtnMp9NY-koHnquZ4CB0Z2sRUjIrw',
            'chenwq28514': 'wDsTRTkM9GUKqHI9HXx7W6kbeWSa3Iqidu_eI6banLT5hW9y4gk5wd3sGn7kNWrUY2-tFzJOb2aTaRv8rJaSHw',
            'dujq47197': 'wDsTRTkM9GUKqHI9HXx7W6kbeWSa3Iqidu_eI6banLSVpSxOP55X8RaCtyBWHGax8n6cPWC7s5WqLnX-YinyFA',
            'gansc21420': 'wDsTRTkM9GUKqHI9HXx7W6kbeWSa3Iqidu_eI6banLRGj6UxSXQDMzrGqp-xaV-PBjpHXc3EPZ4Y5AteSqnblQ',
            'huzh32405': 'wDsTRTkM9GUKqHI9HXx7W6kbeWSa3Iqidu_eI6banLRtnLTGVt_TDj-GMwoYoV6NRI3VZYNgBrv9MbHocjje9g',
            'lijl31833': 'wDsTRTkM9GUKqHI9HXx7W6kbeWSa3Iqidu_eI6banLQ92B0JwGK04RM9aOq2aVfYgoU7aFm3CuV33ujJEs0vCg',
            'zhangxl29470': 'wDsTRTkM9GUKqHI9HXx7W6kbeWSa3Iqidu_eI6banLQZqLnDUK52pa8hGpiycGLKOi0u4_spUCjQPdL1JyiNUg',
            'zhangyuan20788': 'wDsTRTkM9GUKqHI9HXx7W6kbeWSa3Iqidu_eI6banLQ6z7DBLKmMnwHd1-qYOkhOrRlm_RTsvWkADYwdX2EhuQ',
            'yejw46507':'wDsTRTkM9GUKqHI9HXx7W6kbeWSa3Iqidu_eI6banLSOUmO6bsoeaHWvxeNmpY7n-Yr4cHnJJWwjDn_wnoN_YQ',
        },
    }
    ENV={
        'nk4.0':{
            # "api_token": "JA8vlvTM4rOrndAWYYp8RytRdr8HA3_dcw_u7iG7g-omBPjxB0Wiv8Ty40jdWaXQ5LhtEB7IaeVQ0qHWIWQKvQ",# blade授权码
            # "account": "shijb46405",  # 授权码用户
            # "caseHeader":"shijb46405",
            # "caseReviewBy":"shijb46405",
            "vid": "71ac15f8ae",
            '前置条件':'user_key_f6342dfec9cc4d70b53f876',
        },
        'nk5.0': {

        },
    }
    URL='http://cloud.blade.hundsun.com/shareInterface/importOfflineCase.json'

    def __init__(self,mode='nk5.0',user='gansc21420'):
        self.__session=session()
        self.mode=mode
        self.templatedata=merge(
            deepcopy(self.__class__.TEMPLATEDATA),
            **self.__class__.ENV[self.mode],
            **{
                'account':user,
                'caseHeader':user,
                'caseReviewBy':user,
                'api_token':self.__class__.USER[mode][user]
            })
        self.templatedata['data'][0]['step'][0]['stepJson'][self.__class__.ENV[self.mode]['前置条件']]=''

    def __merge(self,**kwargs):
        '''
        参数替换
        '''
        if '前置条件' in kwargs:
            kwargs[self.__class__.ENV[self.mode]['前置条件']]=kwargs.pop('前置条件')
        return merge(deepcopy(self.templatedata),**kwargs)

    def __readExcel(self,xlsxName,sheetName):
        _data={}
        self.datas = OperationExcel(xlsxName, sheetName).getExcelDatas()
        for i in range(self.datas):
            key = self.datas[ExcelVarles.case_name].replace('%','%25'),
            _data[key]={
                'stepDes':self.datas[ExcelVarles.case_describe].replace('%','%25'),
                'inputData':self.datas[ExcelVarles.case_data].replace('%','%25'),
                '前置条件':self.datas[ExcelVarles.case_before].replace('%','%25'),
                'Expect':self.datas[ExcelVarles.case_result].replace('%','%25'),
            }
        return _data

    def run_excel(self,xlsxName,sheetName,**kwargs):
        '''
        以excel运行
        '''
        _nodePath=kwargs.pop('_nodePath')
        for k,v in self.__readExcel(xlsxName,sheetName).items():
            kwargs['nodePath']=_nodePath+f'{k}/场景测试/'
            kwargs={**kwargs,**v}
            # with open('./log.txt','a+') as f:
            #     f.write(str(kwargs)+'\n')
            self.run_single(**kwargs)

    def run_single(self,**kwargs):
        '''
        单个运行
        '''
        response=self.__session.post(url=self.__class__.URL,json=self.__merge(**kwargs))
        # with open('./log.txt', 'a+') as f:
        #     f.write(str(response.json()) + '\n')
        print(response.json())

if __name__ == '__main__':
    data={
        # '_nodePath':'/系统用例/01各业务测试用例/投资业务/STM/指标/',# 路径
        '_nodePath':'/用例转化-模板/风险项预警查询/查询',
        "caseName": "场景1",  # 案例名
        "caseType": "2",  # 案例类型 1-手工,2-自动化
        "casePrior": "P2",  # 优先级P0-3
        "caseSideType": "0",  # 正例 or 反例
        "caseDetailType": "0",  # 用例类型 0-功能,1-性能,2-文档,3-安全,4-兼容性,5-可靠性,6-用户体验,7-安装部署
        "caseLayer": "1",  # 用例分层 0-系统级,1-模块级
    }
    upload=BladeUpload('nk5.0',user='yejw46507')
    # upload.run_single(**data)
    upload.run_excel("C:\\Users\\yejw46507\\Desktop\\FCM导入用例.xlsx",**data)
    # upload._BladeUpload__readExcel("E:\SVN\新建文件夹 (2)\投资业务\测试用例-2022\SPIFA金融期货资管测试用例.xlsx")
